<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>层数最深叶子节点的和</title>
</head>

<body>
  <script>
    var deepestLeavesSum = function (root) {
      if (!root) return 0
      let cur = [root]
      while (cur.length) {
        const temp = []
        cur.map(n => [n.left, n.right].filter(Boolean)).forEach(a => temp.push(...a))
        if (!temp.length) {
          return cur.reduce((a, v) => a + v.val, 0)
        }
        cur = temp
      }
      return 0;
    }
    console.log(deepestLeavesSum([6, 7, 8, 2, 7, 1, 3, 9, null, 1, 4, null, null, null, 5]));
  </script>
</body>

</html>